package com.dd.consulprovider.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dd.consulprovider.dto.UserDto;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Value("${instanceId}")
	private String instanceId;
	
	Logger logger = LoggerFactory.getLogger(UserController.class);
	
	List<UserDto> userList = new ArrayList<UserDto>();
	
	public UserController(){
		for (int i = 0; i < 30; i++) {
			UserDto user = new UserDto(Long.valueOf((i+1)),"user"+(i+1),"password"+(i+1));
			userList.add(user);
		}
	}

	@ApiOperation(value="获取用户列表", notes="")
	@GetMapping("/list")
	public List<UserDto> list(){
		return userList;
	}
	
	@ApiOperation(value="查询用户", notes="根据User对象创建用户")
	@GetMapping("/getUser/{id}")
	public UserDto getUser(@PathVariable Long id){
		logger.info("instanceId="+ this.instanceId + "，getUser传入参数为："+id);
		for (UserDto user : userList) {
			if(user.getId().equals(id)){
				return user;
			}
		}
		return null;
	}
	
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDto")
	@PostMapping("/add")
	public boolean add(@RequestBody UserDto user){
		userList.add(user);
		return true;
	}
	
	@ApiOperation(value="修改用户", notes="根据User对象修改用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDto")
	@PostMapping("/update")
	public boolean update(@RequestBody UserDto user){
		for (UserDto userDto : userList) {
			if(user.getId().equals(userDto.getId())){
				userDto.setUsenrame(user.getUsenrame());
				userDto.setPassword(user.getPassword());
			}
		}
		return true;
	}
	
	@ApiOperation(value="删除用户", notes="根据ID删除用户")
	@PostMapping("/delete/{id}")
	public boolean delete(@PathVariable Long id){
		for (UserDto user : userList) {
			if(user.getId().equals(id)){
				userList.remove(user);
				return true;
			}
		}
		return false;
	}
	
	//延时测试,随机延时0-3秒之间
	@ApiOperation(value="查询用户", notes="根据User对象创建用户")
	@GetMapping("/getUserTimeOut/{id}")
	public UserDto getUserTimeOut(@PathVariable Long id){
		int sleepTime = new Random().nextInt(3000);
		logger.info("instanceId="+ this.instanceId + "，getUserTimeOut传入参数为："+id+" 休眠："+sleepTime);
		try {
			Thread.sleep(sleepTime);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (UserDto user : userList) {
			if(user.getId().equals(id)){
				return user;
			}
		}
		return null;
	}
}
